﻿using SqlSugar;
using System;
using System.Configuration;

namespace Ozone.Help
{
    public sealed class SqlSugarOper : IDisposable
    {
        //线程单例
        private static readonly Lazy<SqlSugarOper> _lazyInstance =
            new Lazy<SqlSugarOper>(() => new SqlSugarOper());
        public static SqlSugarOper Instance => _lazyInstance.Value;
        private readonly SqlSugarScope _sqlSugarScope;
        //私有构造函数（防止外部实例化）
        private SqlSugarOper()
        {
            try
            {
                string connectionString = GetConnectionString("HEO");
                _sqlSugarScope = new SqlSugarScope(new ConnectionConfig()
                {
                    ConnectionString = connectionString,
                    DbType = DbType.MySql,
                    IsAutoCloseConnection = true,      // 自动释放连接
                    InitKeyType = InitKeyType.Attribute,
                },
                db =>
                {
                    // 全局配置
                    //db.Aop.OnLogExecuting = (sql, pars) =>
                    //{
                    //    Console.WriteLine($"执行SQL: {sql}");
                    //};
                });
            }
            catch (Exception ex)
            {
                LogHelper.Error("SqlSugar 初始化失败",ex);
            }
        }

        // 4. 获取连接字符串（带异常处理）
        private static string GetConnectionString(string name)
        {
            var connectionString = ConfigurationManager.ConnectionStrings[name]?.ConnectionString;
            if (string.IsNullOrWhiteSpace(connectionString))
            {
                LogHelper.Debug("数据库连接字符串" + name + "未配置或为空");
            }
            return connectionString;
        }
        public SqlSugarScope GetDbClient() => _sqlSugarScope;
        public void Dispose()
        {
            _sqlSugarScope?.Dispose();
        }
    }

    #region  第二种SqlSugarClient连接
    //public class SqlSugarOper
    //{

    //    private static SqlSugarOper _Instance;
    //    public static SqlSugarOper Instance
    //    {
    //        get
    //        {
    //            if (_Instance == null)
    //            {
    //                _Instance = new SqlSugarOper();
    //            }
    //            return _Instance;
    //        }
    //    }

    //    protected SqlSugarClient db;
    //    /// <summary>
    //    /// 获取数据库连接字段
    //    /// </summary>
    //    private string connectionStr
    //    {

    //        get
    //        {
    //            string ConString = "";

    //            foreach (ConnectionStringSettings item in ConfigurationManager.ConnectionStrings)
    //            {
    //                if (item.Name == "HEO")
    //                {
    //                    ConString = item.ConnectionString;
    //                }
    //            }
    //            return ConString;
    //        }
    //    }

    //    public SqlSugarClient GetSqlSugarClient()
    //    {
    //        SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
    //        {
    //            ConnectionString = connectionStr,
    //            DbType = SqlSugar.DbType.MySql,
    //            IsAutoCloseConnection = true,//自动关闭连接
    //            InitKeyType = InitKeyType.Attribute
    //        });
    //        return db;
    //    }
    //}
    #endregion
}
